#include "GameVScrollBar.h"
#include "MyRender.h"
#include "MyInputMgr.h"
extern CMyRender g_kRender;
extern CMyInputMgr g_kInputMgr;

CGameVScrollBar::CGameVScrollBar(void)
{
	m_eWndType = WT_V_SCROLL_BAR;
	m_iCurPos = 0;
	m_iMaxRange = 6;
	m_pkBtnUp = m_pkBtnMid = m_pkBtnDown = NULL;
	m_bBtnMidDown = false;
	m_iOffsetY = 0;
	m_dwUpdateBarTime = 0;
}

CGameVScrollBar::~CGameVScrollBar(void)
{
}

void CGameVScrollBar::OnCreate()
{
	m_pkBtnUp = new CGameButton();
	m_pkBtnUp->Create("btUp",0,0,1,1,this);

	m_pkBtnDown = new CGameButton();
	m_pkBtnDown->Create("btDown",0,0,1,1,this);

	m_pkBtnMid = new CGameButton();
	m_pkBtnMid->Create("btMid",0,0,1,1,this);
}

void CGameVScrollBar::Init( int iUpH,int iDownH )
{
	if (iUpH + iDownH >= m_iHeight)
	{
		assert(0);
		iUpH = iDownH = (int)((m_iHeight - 4) / 2.0f);
	}

	m_pkBtnUp->ReSize(m_iWidth,iUpH);
	m_pkBtnDown->ReSize(m_iWidth,iDownH);
	m_pkBtnMid->ReSize(m_iWidth,m_iHeight - iUpH - iDownH);

	m_pkBtnUp->Move(0,0);
	m_pkBtnDown->Move(0,m_iHeight - iDownH);
	m_pkBtnMid->Move(0,iUpH);

	UpdateBar();
	return;
}

void CGameVScrollBar::LoadResFromDir(const string& rkDir)
{
	m_pkBtnUp->LoadResFromFile(rkDir + "/" + "Up.bmp");
	m_pkBtnDown->LoadResFromFile(rkDir + "/" + "Down.bmp");
	m_pkBtnMid->LoadResFromFile(rkDir + "/" + "Mid.bmp");

	return;
}

void CGameVScrollBar::LoadResFromFile( const string& rkFile )
{
	CMyImage kImage;
	kImage.Load(rkFile);

	int iW = kImage.GetWidth() / 3;
	int iH = kImage.GetHeight();

	CMyImage kImage2;
	kImage2.LoadFromImage(kImage,0,0,iW,iH);
	m_pkBtnUp->LoadResFormImage(kImage2);

	kImage2.LoadFromImage(kImage,iW,0,iW,iH);
	m_pkBtnDown->LoadResFormImage(kImage2);

	kImage2.LoadFromImage(kImage,iW * 2,0,iW,iH);
	m_pkBtnMid->LoadResFormImage(kImage2);

	return;
}

bool CGameVScrollBar::OnLeftBtnDown( int iX,int iY )
{
	return true;
}

bool CGameVScrollBar::OnLeftBtnUp( int iX,int iY )
{
	return true;
}

bool CGameVScrollBar::OnMidBtnRoll( int iRollDelta,int iX,int iY )
{
	if (m_spkFocusControl != this)
	{
		return false;
	}

	int iStep = abs(iRollDelta);
	if (iRollDelta > 0)
	{
		GoUp(iStep);
	}
	else
	{
		GoDown(iStep);
	}

	return true;
}

void CGameVScrollBar::OnUpdate()
{	
	int iH = m_iHeight - m_pkBtnUp->GetHeight() - m_pkBtnDown->GetHeight();
	float fH = (float)iH / (float)m_iMaxRange;

	if (g_kInputMgr.IsLeftBtnDown())
	{
		if (m_bBtnMidDown)
		{
			int iAbsY = g_kInputMgr.GetMouseY() - m_iOffsetY;			
			int iY = iAbsY - m_iAbsY;		
			Region(iY,m_pkBtnUp->GetHeight(),(int)(m_iHeight - m_pkBtnDown->GetHeight() - fH));
			m_pkBtnMid->Move(0,iY);

			int iPos = (int)((iY - m_pkBtnUp->GetHeight()) / fH);
			Region(iPos,0,m_iMaxRange - 1);		

			if (iPos != m_iCurPos)
			{
				m_iCurPos = iPos;
				OnBarChange();
			}
		}
		else if (IsMouseOn() && g_kInputMgr.GetMouseY() < m_pkBtnMid->GetPosY())
		{
			if (m_dwUpdateBarTime == 0)
			{
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}

			if (GetTickCount() > m_dwUpdateBarTime)
			{
				GoUp();
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}		
		}
		else if (IsMouseOn() && g_kInputMgr.GetMouseY() > m_pkBtnMid->GetPosY() + m_pkBtnMid->GetHeight())
		{
			if (m_dwUpdateBarTime == 0)
			{
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}

			if (GetTickCount() > m_dwUpdateBarTime)
			{
				GoDown();
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}
		}		
	}
	else
	{
		m_bBtnMidDown = false;
		m_iOffsetY = 0;
		m_dwUpdateBarTime = 0;
	}

	
// 	m_pkBtnMid->ReSize(m_iWidth,fH);
// 	m_pkBtnMid->Move(0,m_iCurPos * fH + m_pkBtnUp->GetHeight());

	return;
}

void CGameVScrollBar::OnSize()
{
	Init(m_pkBtnUp->GetHeight(),m_pkBtnDown->GetHeight());
	m_bBtnMidDown = false;
	m_iOffsetY = 0;
	m_dwUpdateBarTime = 0;
	return;
}

bool CGameVScrollBar::OnChildMsg( ChildMsg eMsgType,const string& rkChildName,const string& rkUserData /*= ""*/ )
{
	if (eMsgType == CM_Click)
	{
		if (rkChildName == m_pkBtnUp->GetName())
		{
			GoUp();
			return true;
		}

		if (rkChildName == m_pkBtnDown->GetName())
		{
			GoDown();
			return true;
		}
	}
	else if (eMsgType == CM_LBtnDown)
	{
		if (rkChildName == "btMid")
		{
			if (m_iMaxRange > 1)
			{
				m_bBtnMidDown = true;
				m_iOffsetY = g_kInputMgr.GetMouseY() - GetChild(rkChildName)->GetPosY();
			}
			else
			{
				m_bBtnMidDown = false;
				m_iOffsetY = 0;
			}
		}
	}	

	return true;
}

void CGameVScrollBar::OnBarChange()
{
	if (m_pkParent)
	{
		m_pkParent->ProcChildMsg(CM_VScrollBarChange,m_kName,m_kUserData);
	}

	return;
}

void CGameVScrollBar::SetRange( int iRange )
{
	if (iRange <= 1)
	{
		iRange = 1;
	}

	m_iMaxRange = iRange;
	UpdateBar();
}

int CGameVScrollBar::GetRange()
{
	return m_iMaxRange;
}

void CGameVScrollBar::SetPos( int iPos )
{
	if (iPos <= 0)
	{
		iPos = 0;
	}

	if (iPos >= m_iMaxRange)
	{
		iPos = m_iMaxRange - 1;
	}

	if (m_iCurPos == iPos)
	{
		return;
	}

	m_iCurPos = iPos;
	UpdateBar();
	OnBarChange();
}

int CGameVScrollBar::GetPos()
{
	return m_iCurPos;
}

void CGameVScrollBar::UpdateBar()
{
	int iH = m_iHeight - m_pkBtnUp->GetHeight() - m_pkBtnDown->GetHeight();
	float fH = (float)iH / (float)m_iMaxRange;	
	m_pkBtnMid->ReSize(m_iWidth,(int)fH);
	m_pkBtnMid->Move(0,(int)(m_iCurPos * fH + m_pkBtnUp->GetHeight()));
}

void CGameVScrollBar::OnDraw()
{
	int iH = m_iHeight - m_pkBtnUp->GetHeight() - m_pkBtnDown->GetHeight();
	float fH = (float)iH / (float)m_iMaxRange;

	for (int i = 0;i < m_iMaxRange;i++)
	{
		g_kRender.DrawFrame(m_iAbsX,(int)(i * fH + m_pkBtnUp->GetPosY() + m_pkBtnUp->GetHeight()),m_iWidth,(int)fH,0xFF00FFFF);
	}
}

void CGameVScrollBar::GoUp(DWORD dwStep/* = 1*/)
{
	m_iCurPos -= dwStep;
	if (m_iCurPos <= 0)
	{
		m_iCurPos = 0;
	}

	UpdateBar();
	OnBarChange();
}

void CGameVScrollBar::GoDown(DWORD dwStep/* = 1*/)
{
	m_iCurPos += dwStep;
	if (m_iCurPos >= m_iMaxRange)
	{
		m_iCurPos = m_iMaxRange - 1;
	}

	UpdateBar();
	OnBarChange();
}

void CGameVScrollBar::GoToMaxPos()
{
	SetPos(m_iMaxRange - 1);
	return;
}